<?php

/* *
 * 功能：支付宝服务器异步通知页面
 * 版本：3.3
 * 日期：2012-07-23
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写,并非一定要使用该代码。
 * 该代码仅供学习和研究支付宝接口使用，只是提供一个参考。


 * ************************页面功能说明*************************
 * 创建该页面文件时，请留心该页面文件中无任何HTML代码及空格。
 * 该页面不能在本机电脑测试，请到服务器上做测试。请确保外部可以访问该页面。
 * 该页面调试工具请使用写文本函数logResult，该函数已被默认关闭，见alipay_notify_class.php中的函数verifyNotify
 * 如果没有收到该页面返回的 success 信息，支付宝会在24小时内按一定的时间策略重发通知
 */

require_once("alipay.config.php");
require_once("lib/alipay_notify.class.php");

$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyNotify();
//$verify_result = 1;

if ($verify_result == 1) {

    //验证成功

    $dsn = 'mysql:host=localhost;dbname=kkmai';
    $username = 'root';
    $passwd = 'eDKmpHzrfkcKrHavT5NLuQ';

    $pdo = new \PDO($dsn, $username, $passwd);
    $pdo->exec('set names utf8');

    //批次号
    $batch_no = $_REQUEST['batch_no'];
    $success_details = $_POST['success_details'];
    $fail_details = $_POST['fail_details'];
    
//    $batch_no = '120191009157058323696';
//    $success_details = '成功了';
//    $fail_details = '失败了';

    if ($success_details) {
        $sql = "SELECT * FROM ai_store_tixian WHERE batch = :batch LIMIT 0, 1";
        $stat = $pdo->prepare($sql);
        $result = $stat->execute([':batch' => $batch_no]);
        if ($result) {
            $row = $stat->fetch(\PDO::FETCH_ASSOC);
            if ($row && $row['status'] == 1) {
                $updateSql = "UPDATE ai_store_tixian SET status = :status, utime = :utime WHERE batch = :batch";
                $updateStat = $pdo->prepare($updateSql);
                $updateResult = $updateStat->execute([
                    ':status' => 2,
                    ':utime' => date('Y-m-d H:i:s'),
                    ':batch' => $batch_no,
                ]);
                
                //更新店铺流水
                $staffFlowRowsql = "SELECT * FROM `ai_store_flow` WHERE `store_id`= :store_id ORDER BY `id` DESC LIMIT 0,1";
                $staffFlowRowstat = $pdo->prepare($staffFlowRowsql);
                $staffFlowRowresult = $staffFlowRowstat->execute([':store_id' => $row['store_id']]);
                $staffFlowRow = $staffFlowRowstat->fetch(\PDO::FETCH_ASSOC);
                if($staffFlowRow){
                    $last_money = $staffFlowRow['last_money'];
                }  else {
                    $last_money = 0;
                }
                
                $insertStaffFlowsql = "INSERT INTO `ai_store_flow`(`item_name`, `type`, `pro_type`, `store_id`, `allmoney`, `money`, `last_money`, `add_time`, `poundage`) VALUES ('提现',2,8,:store_id,:allmoney,:money,:last_money,:add_time,:poundage)";
                $insertStaffFlowstat = $pdo->prepare($insertStaffFlowsql);
                $insertStaffFlowresult = $insertStaffFlowstat->execute([
                    ':store_id' => $row['store_id'],
                    ':allmoney' => $row['money'] + $row['poundage'],
                    ':money' => $row['money'] + $row['poundage'],
                    ':last_money' => $last_money - $row['money'] - $row['poundage'],
                    ':add_time' => date('Y-m-d H:i:s'),
                    ':poundage' => $row['poundage'],
                ]);

                if ($updateResult && $insertStaffFlowresult) {
                    $storesql = "SELECT store_keeper,keeper_phone,money,tx_money FROM ai_store WHERE id = :id LIMIT 0, 1";
                    $storestat = $pdo->prepare($storesql);
                    $result = $storestat->execute([':id' => $row['store_id']]);
                    $storerow = $storestat->fetch(\PDO::FETCH_ASSOC);
                    //查询店铺老板id
                    $storeStaffSql = "SELECT staff_id FROM ai_store_staff WHERE store_id = :id LIMIT 0,1";
                    $storeStaffstat = $pdo->prepare($storeStaffSql);
                    $result = $storeStaffstat->execute([':id' => $row['store_id']]);
                    $storeStaffrow = $storeStaffstat->fetch(\PDO::FETCH_ASSOC);

                    $ApiPath = 'http://ai.app.dwkkm.com/';
                    $staffName = $storerow['store_keeper'];
                    $tixianMoney = $row['money'] / 100;
                    $money = $storerow['money'] / 100;
                    $all = ($storerow['money'] + $storerow['tx_money']) / 100;
                    $tx_money = $storerow['tx_money'] / 100;

                    $postFields['content'] = '【提现成功】尊敬的' . $staffName . '，您的提现金额' . $tixianMoney . '已到账，请及时查收，您的账户余额' . $money . '，您总计营收' . $all . '，已提现完成总计' . $tx_money;
                    $postFields['type'] = 7; //0新店上新7提现
                    $postFields['titleId'] = 3; //1店铺动态2预约消息3营收消息
                    $postFields['otherId'] = 0; //新店上新就是0
                    $postFields['staffId'] = $storeStaffrow['staff_id'];
                    $postFields['storeId'] = $row['store_id'];
                    $postFields['phone'] = $storerow['keeper_phone'];
                    $url = $ApiPath . 'push/pushMessage/';
                    $headers = array(
                        'Content-type:application/json;charset=utf-8',
                        'Accept:application/json',
                        'Cache-Control: no-cache',
                        'Pragma: no-cache'
                    );
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    $result = curl_exec($ch);
                    curl_close($ch);
//                    print_r(json_encode($postFields));
                    echo 'success';
                } else {
                    echo 'fail1';
                }
            } else {
                echo 'fail2';
            }
        }  else {
            echo 'fail3';
        }
    } else {
        echo $fail_details;
    }
} else {
    echo "fail4";
}
?>
